/**
 * Created by Arnald on 2017/6/6.
 */
let userDao = require('../mysql/userDao.js');
let timesDao = require('../mysql/timesDao.js');
let publicDao = require('../mysql/publicDao.js');
let msg = require('../util/msg.js');
let recharge = require('./recharge.js');
let mail = require('./mail.js');
let info = require('./info.js');
let variable = require('../data/variable.js');
let rpc = require('../util/rpc.js');
let gameData = require('../util/gameData.js');

module.exports = action;

function action(obj, cid) {
    if (!obj.userId) {
        msg.send(cid, obj.uuid, 'error', {'api': obj.action, 'code': 10018});
        return false;
    }
    let data = obj.data;
    switch (obj.action) {
        case 'funcUserInfo':
            if (!data.id) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcUserInfo', 'code': 10061});
                return false;
            }
            info.search(obj.userId, obj.uuid, cid, data);
            break;
        case 'funcUserFollow':
            info.follow(obj.userId, obj.uuid, cid, data);
            break;
        case 'funcUserUnFollow':
            info.unFollow(obj.userId, obj.uuid, cid, data);
            break;
        case 'funcRanking':
            publicDao.init().table('user').pluck('id', 'name', 'avatar', 'playVictory', 'playWins')
                .limit(30).orders('playWins', 'DESC').get(function (err, res) {
                if (err !== null) {
                    return false;
                } else if (res.length == 0) {
                    return false;
                } else {
                    cb(res);
                }
            });
            break;
        case 'funcGoods':
            //if (!data.platform) {
            //    msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 11003});
            //    return false;
            //}
            recharge.goods(obj.userId,cid, obj.uuid, data.platform);
            break;
        case 'funcRecharge':
            if (!gameData.d.config.recharge) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 10084});
                return false;
            }
            // if (!data.accessToken) {
            //     msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 11003});
            //     return false;
            // }
            if (!data.weixinId) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 11005});
                return false;
            }   
            if (!data.goodsId) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 10064});
                return false;
            }
            if (!gameData.d.goods[data.goodsId]) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcRecharge', 'code': 10065});
                return false;
            }   
            let rechargeClass = new recharge(obj.userId, data.weixinId, cid, obj.uuid, data.goodsId);
            rechargeClass.run();
            break;
        case 'funcMailList':
            mail.list(obj.userId, cid, obj.uuid);
            break;
        case 'funcMailTake':
            if (!data.id) {
                msg.send(cid, obj.uuid, 'error', {'api': 'funcMailTake', 'code': 10067});
            }
            mail.take(obj.userId, data.id, cid, obj.uuid);
            break;
        case 'funcMailTakeAll':
            mail.takeAll(obj.userId, cid, obj.uuid);
            break;
        case 'funcGameAmount':
            rpc.dc.send('getGameAmount', null, function (result) {
                msg.send(cid, obj.uuid, 'funcGameAmount', result);
            });
            break;
        case 'funcGiveGift':
            mail.giveGift(obj.userId, cid, obj.uuid, data);
            break;
        case 'funcDailySignIn':
            mail.dailySignIn(obj.userId, cid, obj.uuid);
            break;
        case 'funcTaskFinish':
            let param = {};
            param.userId = obj.userId;
            param.taskId = data.taskId;
            rpc.dc.send('taskFinish', param);
            break;
        case 'funcOnlineReward':
            mail.onlineReward(obj.userId, cid, obj.uuid);
            break;
        case 'funcLoserSupport':
            let timesModel = new timesDao();
            timesModel.inId(obj.userId, function (err, res) {
                if (gameData.d.loserSupport.length <= timesModel.data.loserSupport) {
                    return false;
                }
                let ls = gameData.d.loserSupport[timesModel.data.loserSupport];
                let userModel = new userDao();
                userModel.inId(obj.userId, function (err, res) {
                    if (res == null) {
                        return false;
                    }
                    let resultData = {};
                    userModel.add('gold', ls.gold);
                    resultData.gold = {'now': userModel.data.gold, 'add': ls.gold};
                    timesModel.set('loserSupport', (timesModel.data.loserSupport + 1));
                    userModel.save(function (err, res) {
                        msg.send(cid, uuid, 'globalUserChange', resultData);
                        msg.send(cid, uuid, 'funcLoserSupport', {'num': gameData.d.loserSupport.length - timesModel.data.loserSupport});
                        msg.send(cid, uuid, 'globalAlert', {'text': ls.msg, 'title': null});
                        timesModel.save();
                    });
                });
            });
            break;
    }
}